package com.bree.erp.jsf.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.PartUnit;
import com.bree.erp.entity.ReturnsBalance;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.service.PartUnitService;
import com.bree.erp.service.ReturnsBalanceService;

@ManagedBean(name="dtPartUnitLazyView")
@ViewScoped
public class PartUnitLazyView {
	
	private static Logger LOG = Logger.getLogger(PartUnitLazyView.class);
    
    @EJB
    private PartUnitService puService;
    
    @EJB
    private ReturnsBalanceService balanceService;

	@ManagedProperty(value="#{dtItemRequestLazyView}")
    private ItemRequestLazyView itemRequestLazyView;

    private LazyDataModel<PartUnit> lazyModel;
    
    private PartUnit selectedItem;
    
    private double quantity;
    
    @PostConstruct
    public void init() {
    	lazyModel = new LazyBreeDataModel<PartUnit, PartUnitService, Long>(puService, 0L);
    }
    
   public void onRowSelect(SelectEvent event) {
       FacesMessage msg = new FacesMessage("Item Selected", ((PartUnit) event.getObject()).getId() + "");
       FacesContext.getCurrentInstance().addMessage(null, msg);
   }
   
   public void addItem() {
	   
	   LOG.debug("PartUnit: " + (this.selectedItem == null ? "<null>" : this.selectedItem.getShortName()) + "; quantity: " + quantity);
	
	   itemRequestLazyView.addItem(selectedItem, quantity);
   }
   
   public Map<Long, Double> getMapBalance() {
	   
	   Map<Long, Double> mapBalance = new HashMap<Long, Double>();
	
	   // TODO: optimization point here!
	   List<ReturnsBalance> list = balanceService.sumPartUnits();
	   for(ReturnsBalance item : list) {
		   mapBalance.put(item.getPartUnit().getId(), item.getTotalBalance());
	   }
	   
	   return mapBalance;
   }
   

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<PartUnit> getLazyModel() {
		return lazyModel;
	}

	/**
	 * @param lazyModel the lazyModel to set
	 */
	public void setLazyModel(LazyDataModel<PartUnit> lazyModel) {
		this.lazyModel = lazyModel;
	}

	/**
	 * @return the selectedItem
	 */
	public PartUnit getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(PartUnit selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the itemRequestLazyView
	 */
	public ItemRequestLazyView getItemRequestLazyView() {
		return itemRequestLazyView;
	}

	/**
	 * @param itemRequestLazyView the itemRequestLazyView to set
	 */
	public void setItemRequestLazyView(ItemRequestLazyView itemRequestLazyView) {
		this.itemRequestLazyView = itemRequestLazyView;
	}
}
